//
//  HMNetworkTools.swift
//  06-Swift-AFN网络封装
//
//  Created by HM on 2017/7/1.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
import AFNetworking
// 请求方式get post 枚举
// 支持多种类型 不单单是int类型
enum HMNetworkToolsType:String {
    case get = "get"
    case post = "post"
}


class HMNetworkTools: AFHTTPSessionManager {

    
    
    // 全局访问点
    static let share: HMNetworkTools = {
        let tools = HMNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    /// 网络请求公共方法封装
    ///
    /// - Parameters:
    ///   - type: 请求方式
    ///   - urlString: 地址
    ///   - parameters: 参数
    ///   - success: 成功的回调
    ///   - failure: 失败的回调
    func request(type: HMNetworkToolsType, urlString: String, parameters: Any?, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 判断请求方式
        // get
        if type == .get {
            get(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response)
            }, failure: { (_, error) in
                failure(error)
            })
            
        }else{
            // post
            post(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response)
            }, failure: { (_, error) in
                failure(error)
            })
        }
    }
}
// OAuth需要的网络接口
extension HMNetworkTools{
    // 请求UserAccount
    func oauthLoadUserAccount(code: String, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求地址
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": HMAPPKEY,
            "client_secret": HMAPPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": HMREDIRECT_URI
        ]
        request(type: HMNetworkToolsType.post, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    // 请求个人信息数据
    func oauthLoadUserInfo(userAccountModel: HMUserAccountModel, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求地址
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token": userAccountModel.access_token,
            "uid": userAccountModel.uid]
        // 发送网络请求
        request(type: HMNetworkToolsType.get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
}
/*
 since_id	false	int64	若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
 max_id	false	int64	若指定此参数，则返回ID小于或等于max_id的微博，默认为0。
 */
//MARK: - 请求首页数据接口
extension HMNetworkTools{
    func homeLoadData(since_id:Int64, max_id: Int64, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求地址
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params = [
            "access_token": HMOAuthViewModel.share.accessToken,
            "since_id": "\(since_id)",
            "max_id": "\(max_id)"]
        
        
        // 发送请求
        request(type: .get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}
